package cn.com.pc.content.service;

import cn.com.pc.content.domain.audit.Auditor;
import cn.com.pc.content.domain.content.Content;
import cn.com.pc.content.domain.content.ContentType;
import cn.com.pc.content.domain.risk.CensorPlatformRiskAuditor;
import cn.com.pc.content.domain.risk.Risk;
import cn.com.pc.content.domain.risk.RiskScenes;
import cn.com.pc.content.util.ContentUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class CensorPlatformService {

    @Autowired
    private RiskService riskService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private CensorPlatformRiskAuditor auditor;

    private static final int BATCH_SIZE = 10;
    private static final int CENSORSYSTEM_RESPONSE_PASS = 2;
    private static final int CENSORSYSTEM_RESPONSE_DENY = 3;

    @Scheduled(cron = "0 * * * * *")
    public void fetchAndSend() {
        List<Risk> list = riskService.fetch(RiskScenes.censorPlatform, BATCH_SIZE);
        for (Risk risk : list) {
            auditor.audit(risk);
        }
    }

    public int censorCallback(String url, int status, long version, Date updateAt) {
        ContentType contentType = ContentUtils.getContentTypeFromUrl(url);
        Content content = contentService.getContentById(ContentUtils.getContentIdFromUrl(url), contentType);
        List<Risk> risks = riskService.getByContentAndScenes(content, RiskScenes.censorPlatform);
        Risk risk = risks.isEmpty() ? new Risk() : risks.get(0);
        risk.setAuditAt(updateAt);
        risk.setAuditor(Auditor.CENSOR_PLATFORM);
        switch (status) {
            case CENSORSYSTEM_RESPONSE_PASS:
                contentService.publishContent(content, contentType);
                break;
            case CENSORSYSTEM_RESPONSE_DENY:
                contentService.rejectContent(content, contentType);
                risk.setRisk(10);
                break;
            default:
                break;
        }
        return riskService.update(risk);
    }


//    /**
//     * 审核应用回馈
//     *
//     * @param url
//     * @param status
//     * @param version
//     * @param updateAt,最后的更新时间,时间的long
//     * @return
//     */
//    public String feedback(String url, int status, long version, Date updateAt) {
//        if (!csr_switch || StringUtils.isBlank(url)) {
//            return null;
//        }
//
//        boolean isUpdate = false;
//        String[] split = url.split("\\?");
//        if (split.length > 1) {
//            url = split[0];
//            if ("update".equals(split[1])) {
//                isUpdate = true;
//            }
//        }
//        String result = null;
//        long[] ids = Functions.parseUrl(url);
//        //记录操作log TODO
//        Post post = postService.findPost(ids[0], ids[1]);
////        if (post == null) {
////            postService.restoreDeletedPost(ids[1], ids[0], updateAt);
////            result = "post restored";
////            return result;
////        }
//
//        log.debug("CENSOR STATUS IS: " + status);
//        System.out.println("status is " + status);
//        if (post != null) {
//            System.out.println("post.status()=" + post.getStatus());
//        }
//        switch (status) {
//            case CENSORSYSTEM_RESPONSE_PASS:
//                if (post == null) {
//                    TopicLog topicLog = logService.findLatestDeletePostLogByPostId(ids[1], updateAt);
//                    if (topicLog != null && topicLog.getAction() == Log.Action.deletePost && topicLog.getOperatorId() == 0l && "审核系统删除".equals(topicLog.getReason())) {
//                        postService.restoreDeletedPost(ids[1], ids[0], updateAt);
//                        result = "post restored";
//                    } else {
//                        log.warn("csr trying to restore a manual deleted post, tid:" + ids[0] + ",pid:" + ids[1]);
//                    }
//                } else if (post.getStatus() == 0) {
//                    censorSuccess(post, isUpdate);
//                } else if (post.getStatus() < 0) {
//                    log.debug("CENSOR FEEDBACK DEGUB, POST STATUS IS: " + post.getStatus());
//                    if (post.getFloor() == 1) {
//                        log.debug("CENSOR FEEDBACK DEGUB, POST FLOOR IS: " + post.getFloor());
//                        TopicLog topicLog = logService.findLatestDeleteTopicLogByTopicId(post.getTid(), updateAt);
//                        if (topicLog != null && topicLog.getAction() == Log.Action.deleteTopic && topicLog.getOperatorId() == 0l && "审核系统删除".equals(topicLog.getReason())) {
//                            System.out.println("begin to restore topic");
//                            result = csrRestoreDeletedTopic(post.getTopic());
//                            boolean showAfterCensorSwitch = systemConfig.getShowAfterCensor() == 1;
//                            if (showAfterCensorSwitch) {
//                                censorSuccess(post, isUpdate);
//                            }
//                        } else {
//                            System.out.println("csr trying to restore a manual deleted topic, tid:" + post.getTid() + ",pid:" + post.getPid());
//                            log.warn("csr trying to restore a manual deleted topic, tid:" + post.getTid() + ",pid:" + post.getPid());
//                        }
//                        log.debug("CENSOR FEEDBACK DEGUB, RESULT IS: " + post.getFloor());
//                    } else {
//                        log.debug("CENSOR FEEDBACK DEGUB, POST FLOOR IS: " + post.getFloor());
//                        //主题被删,恢复某个回帖,暂时不进行处理,处理结束无意义(?)
//                        //这里有可能使得审核平台与论坛status不统一
//                    }
//                }
//                break;
//            case CENSORSYSTEM_RESPONSE_DENY:
//                if (post == null) {
//                    result = "帖子已删除";
//                } else if (post.getStatus() >= 0) {
//                    if (post.getFloor() == 1) {
//                        result = csrDeleteTopic(post.getTopic());
//                    } else {
//                        result = csrDeletePost(post);
//                    }
//                } else {
//                    postDao.deletePostToCensor(post);
//                }
//                break;
//            default:
//                result = "状态不正确";
//                break;
//        }
//        System.out.println("resutl=" + result);
//        return result;
//    }

}
